<?php
namespace app\api\controller;
use app\common\controller\BaseApi;

/*动狗科技 接口 api*/
class DongGouApi extends  BaseApi
{
	//获取AccessToken
	const AccessToken="https://openapi.gymsmart.cn/base/access_token";
	//获取场景Token
	const SceneToken="https://openapi.gymsmart.cn/base/scene_token";
	//开始投影
	const Match="https://openapi.gymsmart.cn/tv/match";
	//结束投影
	const End="https://openapi.gymsmart.cn/tv/end";
	//获取用户锻炼历史记录
	const History="https://openapi.gymsmart.cn/data/history";
	//获取单次锻炼详细
	const Detail="https://openapi.gymsmart.cn/data/detail";
	//获取最后一次锻炼详细
	const Last="https://openapi.gymsmart.cn/data/last";
	//按时间获取锻炼历史
	const HistoryF2="https://openapi.gymsmart.cn/data/history_f2";
	
	//配置代码
	const open_id="gymsmart95bc57mr";
	const secret="0li7pqk8dez8lk0nqlqqcan7ls5ex1dq";
	const account="10012372";
	const pwd="554086";
	
	/**
	 * 测试地址
	 * */
	 public function _initialize()
    {	
    }
	/**
	 * smartgym.com/api/DongGouApi/test
	 * */
	public function test(){
//		var_dump($this->_AccessToken());die;
//		var_dump($this->_SceneToken());die;
//		var_dump(post_json_test());die;
//		var_dump($this->Match());
//		var_dump($this->End());
  		var_dump($this->Histroy());
	}
	/**
	 * 获取token
	 * {
		    "status":1,
		    "access_token":"359fe68e22e08dd5b6f1a6a03940d7c0",
		    "expires_in":7200
		}
	 */
    function _AccessToken(){
		$res=post_json(self::AccessToken,array("open-id"=>self::open_id,"secret"=>self::secret));
		return $this->_varify_res($res);
    }
	/**
	 * 获取场景id
	 * {
		    "status":1,
		    "scene_id":1,
		    "scene_token":"1309071d380c28930e365664ec37f12f",
		    "expires_in":7200
		}
	 * */
	function _SceneToken(){
		$res=post_json(self::SceneToken,array("account"=>self::account,"pwd"=>self::pwd));
		$this->_varify_res($res);
		return $res;
	}
	/**
	 * 投影视屏
	 * {
   		 "status":1
	   }
	 * */
	function Match(){
		$mobile=input("mobile","15202373874");
		$heart_number=input("heart_number","0445354");
		$data['user']=array(
			"mobile"=>$mobile,
			"nickname"=>$mobile,
			"gender"=>1,
			"weight"=>750,
			"height"=>170,
			"birthday"=>"1990-07-12",
			"avatar"=>"https://s3.cn-north-1.amazonaws.com.cn/res2.gymsmart.cn/user_avatar/2000/958_2947cc83-0eab-43ca-bf8e-2a632e1b7f8b.jpg",
		);
		$data['heart_number']=$heart_number;
		$data['tv_number']=12372;
		return $this->_varify_res(post_json(self::Match,$data,$this->getHeader()));
	}
	
	/**
	 * 结束投影
	 * {
    	"status":1
		}
	 * */
	function End(){
		$mobile=input("mobile","15202373874");
		$data['mobile']=$mobile;
		$data['tv_number']=12372;
		$data_string=json_encode($data,TRUE);
		return $this->_varify_res(post_json(self::End,$data,$this->getHeader()));
		
	}
	
	/**
	 * 获取历史数据
	 * 
	 * {
    	"status":1,
		    "list":[
		        {
		            "logid":130063,
		            "calorie":12,
		            "ave_hr":110,
		            "elapsed":132,
		            "ts":"2017-09-25T07:47:17.000Z"
		        }
		    ]
		}
	 * */
	function Histroy(){
		$mobile=input("mobile","15202373874");
		$data['mobile']=$mobile;
		$data['pageCount']=20;
		$data['logid']=0;
		return $this->_varify_res(get_json(self::History,$data,$this->getHeader()));
	}
	
	/**
	 * 获取历史数据详情
	 * */
	function Detail(){
		$mobile=input("mobile","15202373874");
		$data['mobile']=$mobile;
		$data['logid']=466098;
		return $this->_varify_res(get_json(self::Detail,$data,$this->getHeader()));
		
	}
	
	/**
	 * 最后一次锻炼详情
	 * */
	function Last(){
		$mobile=input("mobile","15202373874");
		$data['mobile']=$mobile;
		return $this->_varify_res(get_json(self::Last,$data,$this->getHeader()));
		
	}
	
	/**
	 * 最后一次锻炼详情
	 * */
	function HistoryF2(){
		$mobile=input("mobile","15202373874");
		$data['mobile']=$mobile;
		$data['start_ts']="2017-01-01 08:00:00";
		$data['end_ts']="2019-01-01 08:00:00";
		return $this->_varify_res(get_json(self::HistoryF2,$data,$this->getHeader()));
		
	}
	/*获取头部*/
	function getHeader(){
		$accesstoken=$this->_AccessToken();
		$sceneToken=$this->_SceneToken();
		return array('open-id: '.self::open_id,'access-token: '.$accesstoken["access_token"],'scene-id: '.$sceneToken["scene_id"],'scene-token: '.$sceneToken["scene_token"]);
	}
	function _varify_res($res){
		if(empty($res)){
			$this->api_ajax_error("","调用ip 不在Gymsmart 白名单中  ".request()->action());
		}
		if($res['status']!=1){
			$this->api_ajax_error("",$this->_error($res['status']));
		}
		return $res;
	}
	function _error($code){
          $error=[
            '1'=>"调用接口成功",
            '1000'=>"服务器未知错误",
            '1001'=>"参数格式错误",
            '1002'=>"无效的开发者凭证",
            '1003'=>"无效的场馆管理账号",
            '1004'=>"没有此接口的调用权限",
            '1005'=>"无效的access_token访问凭证",
            '1006'=>'无效的scene_token场馆凭证',
            '1007'=>'接收器未开机或者已掉线',
            '1008'=>'用户重复投射',
          ];
		if(in_array($code,array_keys($error))){
			return $error[$code];
		}else{
			return "错位类型不存在";
		}		
	}
}
